package com.example.pdf;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;

/**
 * Description: PdfUtils <br>
 * 依赖的包：itextpdf itext-asian commons-io,commons-codec
 * 
 * @author mk
 * @Date 2018-11-2 14:32 <br>
 * @Param
 * @return
 */
public class GenPDF {

	private static final String sourceFile = "D:\\workspace\\xjrccb\\corbank\\WebContent\\download\\wusuWaterBatchPrint.pdf";
	private static final String sourceFile1 = "D:\\workspace\\xjrccb\\corbank\\WebContent\\download\\wusuWaterBatchPrint1.pdf";

	public static void main(String[] args) throws IOException {
		Map<String, String> map = new HashMap<String, String>();
		map.put("ticketNo", "WSSL2020090700001");
		map.put("transDatePage", "2020年09月07日");
		map.put("payaccountName", "付款人全称");
		map.put("pubaccountName", "收款人全称");
		map.put("payaccount", "付款人账号");
		map.put("pubaccount", "收款人账号");
		map.put("payBankName", "付款人账号开户行");
		map.put("trfRecbankname", "收款人账号开户行");
		map.put("wordsAmount", "大写金额");
		map.put("money", "小写金额");
		map.put("projectNum", "项目编号");
		map.put("jjNum", "机井编号");
		map.put("unitName", "项目名称");
		map.put("waterVal", "数量");
		map.put("waterPrice", "标准");
		map.put("waterFee", "金额");

		map.put("ticketNo1", "WSSL2020090700001");
		map.put("transDatePage1", "2020年09月07日");
		map.put("payaccountName1", "付款人全称");
		map.put("pubaccountName1", "收款人全称");
		map.put("payaccount1", "付款人账号");
		map.put("pubaccount1", "收款人账号");
		map.put("payBankName1", "付款人账号开户行");
		map.put("trfRecbankname1", "收款人账号开户行");
		map.put("wordsAmount1", "大写金额");
		map.put("money1", "小写金额");
		map.put("projectNum1", "项目编号");
		map.put("jjNum1", "机井编号");
		map.put("unitName1", "项目名称");
		map.put("waterVal1", "数量");
		map.put("waterPrice1", "标准");
		map.put("waterFee1", "金额");

		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		for (int i = 0; i < 3; i++) {
			list.add(map);
		}
		String targetFile = "D:\\tmp\\agreementTemplate.pdf";
		createPdf(list, targetFile);
	}

	public static void createPdf(List<Map<String, String>> datas, String contractFileName) {
		// 每一条数据代表一个pdf表格
		FileOutputStream fos = null;
		List<PdfReader> list = new ArrayList<PdfReader>();
		try {
			fos = new FileOutputStream(contractFileName);
			int tempPageNum = datas.size() % 2;
			String templatePath = sourceFile1;
			for (int i = 0; i < datas.size(); i++) {
				Map<String, String> data = datas.get(i);
				if (tempPageNum != 0) {
					if (i == datas.size() - 1) {
						templatePath = sourceFile;
					}
				}
				BaseFont chinessFont = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
				PdfReader reader = new PdfReader(templatePath);
				ByteArrayOutputStream bos = new ByteArrayOutputStream();
				PdfStamper ps = new PdfStamper(reader, bos);
				AcroFields acroFields = ps.getAcroFields();
				for (String key : acroFields.getFields().keySet()) {
					acroFields.setFieldProperty(key, "textfont", chinessFont, null);
					acroFields.setFieldProperty(key, "textsize", new Float(12), null);
				}
				for (String fieldName : data.keySet()) {
					acroFields.setField(fieldName, data.get(fieldName));
				}
				ps.close();
				PdfReader pdfReader = new PdfReader(bos.toByteArray());
				list.add(pdfReader);
			}
			Document document = new Document();
			PdfCopy copy = new PdfCopy(document, fos);
			document.open();
			for (int k = 0; k < list.size(); k++) {
				PdfReader pdfReader = list.get(k);
				document.newPage();
				copy.addDocument(pdfReader);
			}
			copy.close();
			document.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}